﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Globalization;

namespace QLDIAOC
{

    public partial class MainWindow
    {
        DataTable m_KhachHangDT;
        DataRowView m_KhachHangDRV = null;
        private void dtgKhachHang_Loaded(object sender, RoutedEventArgs e)
        {
            LoadTableKhachHang();
            cbbMaKhachHang_KhachHang.DataContext = m_KhachHangDT;
        }
        private void cbbMaKhachHang_KhachHang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ResetTrangThaiTextBox();

        }
        public void ShowKhachHangFromMaKH()
        {
            m_GetData.m_SqlCommand = new SqlCommand("SHOW_KHACH_HANG_FROM_MAKH", m_GetData.m_SqlConnection);
            m_GetData.m_SqlCommand.CommandType = CommandType.StoredProcedure;
            m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@MaKH", SqlDbType.Char, 10));
            m_GetData.m_SqlCommand.Parameters["@MaKH"].Value = cbbMaKhachHang_KhachHang.Text;
            m_GetData.m_SqlDataReader = m_GetData.m_SqlCommand.ExecuteReader();
            if (m_GetData.m_SqlDataReader.Read())
            {
                txbDiaChi_KhachHang.Text = m_GetData.m_SqlDataReader["DiaChi"].ToString();
                txbTenKhachHang_KhachHang.Text = m_GetData.m_SqlDataReader["TenKH"].ToString();
                txbSDT_KhachHang.Text = m_GetData.m_SqlDataReader["SDT"].ToString();

            }
            m_GetData.m_SqlCommand.Dispose();
            m_GetData.m_SqlDataReader.Dispose();

        }
        private void btnThem_KhachHang_Click(object sender, RoutedEventArgs e)
        {
            if (!nameEx.Match(txbTenKhachHang_KhachHang.Text).Success)
            {
                MessageBox.Show("Tên không được trống và không chứa các ký tự đặc biệt, các chữ số");
                txbTenKhachHang_KhachHang.Foreground = Brushes.Red;
                return;
            }
            else
                txbTenKhachHang_KhachHang.Foreground = Brushes.Black;
            if (!numberEx.Match(txbSDT_KhachHang.Text).Success)
            {
                MessageBox.Show("Số điện thoại không được trống và chỉ chứa số");
                txbSDT_KhachHang.Foreground = Brushes.Red;
                return;
            }
            else
                txbSDT_KhachHang.Foreground = Brushes.Black;

            m_GetData.m_SqlCommand = new SqlCommand("ADD_KHACH_HANG", m_GetData.m_SqlConnection);
            m_GetData.m_SqlCommand.CommandType = CommandType.StoredProcedure;
            m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@TenKH", SqlDbType.NVarChar, 30));
            m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.NVarChar, 50));
            m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@SDT", SqlDbType.VarChar, 20));
            m_GetData.m_SqlCommand.Parameters["@TenKH"].Value = txbTenKhachHang_KhachHang.Text;
            m_GetData.m_SqlCommand.Parameters["@DiaChi"].Value = txbDiaChi_KhachHang.Text;
            m_GetData.m_SqlCommand.Parameters["@SDT"].Value = txbSDT_KhachHang.Text;
            m_GetData.m_SqlDataReader = m_GetData.m_SqlCommand.ExecuteReader();
            m_GetData.m_SqlDataReader.Dispose();

            LoadTableKhachHang();
            ResetTrangThaiTextBox();
            MessageBox.Show("Đã thêm xong");

        }
        private void btnXem_KhachHang_Click(object sender, RoutedEventArgs e)
        {
            ShowKhachHangFromMaKH();
            txbSDT_KhachHang.Foreground = Brushes.Black;
            txbTenKhachHang_KhachHang.Foreground = Brushes.Black;
        }
        public void LoadTableKhachHang()
        {
            m_KhachHangDT = m_GetData.MakeTable("SHOW_KHACH_HANG", "KHACH_HANG");
            dtgKhachHang.ItemsSource = m_KhachHangDT.DefaultView;
        }
        private void btnSua_KhachHang_Click(object sender, RoutedEventArgs e)
        {
            if (!nameEx.Match(txbTenKhachHang_KhachHang.Text).Success)
            {
                MessageBox.Show("Tên không được trống và không chứa các ký tự đặc biệt, các chữ số");
                txbTenKhachHang_KhachHang.Foreground = Brushes.Red;
                return;
            }
            else
                txbTenKhachHang_KhachHang.Foreground = Brushes.Black;
            if (!numberEx.Match(txbSDT_KhachHang.Text).Success)
            {
                MessageBox.Show("Số điện thoại không được trống và chỉ chứa số");
                txbSDT_KhachHang.Foreground = Brushes.Red;
                return;
            }
            else
                txbSDT_KhachHang.Foreground = Brushes.Black;
            try
            {
                m_GetData.m_SqlCommand = new SqlCommand("UPDATE_KHACH_HANG", m_GetData.m_SqlConnection);
                m_GetData.m_SqlCommand.CommandType = CommandType.StoredProcedure;
                m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@MaKH", SqlDbType.Char, 10));
                m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@TenKH", SqlDbType.NVarChar, 30));
                m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@DiaChi", SqlDbType.NVarChar, 50));
                m_GetData.m_SqlCommand.Parameters.Add(new SqlParameter("@SDT", SqlDbType.VarChar, 20));
                m_GetData.m_SqlCommand.Parameters["@MaKH"].Value = cbbMaKhachHang_KhachHang.Text;
                m_GetData.m_SqlCommand.Parameters["@TenKH"].Value = txbTenKhachHang_KhachHang.Text;
                m_GetData.m_SqlCommand.Parameters["@DiaChi"].Value = txbDiaChi_KhachHang.Text;
                m_GetData.m_SqlCommand.Parameters["@SDT"].Value = txbSDT_KhachHang.Text;
                m_GetData.m_SqlDataReader = m_GetData.m_SqlCommand.ExecuteReader();
                m_GetData.m_SqlDataReader.Dispose();

                LoadTableKhachHang();
                ResetTrangThaiTextBox();
                MessageBox.Show("Đã sửa xong");

            }
            catch (SqlException es)
            {
                MessageBox.Show(es.Message.ToString());
            }
        }
        public void ResetTrangThaiTextBox()
        {
            txbDiaChi_KhachHang.Text = "";
            txbTenKhachHang_KhachHang.Text = "";
            txbSDT_KhachHang.Text = "";
            
        }
        public void ResetTrangThaiKhachHang()
        {
            cbbMaKhachHang_KhachHang.Visibility = Visibility.Hidden;
            btnSua_KhachHang.Visibility = Visibility.Hidden;
            btnThem_KhachHang.Visibility = Visibility.Hidden;
            btnNext_KhachHang.Visibility = Visibility.Hidden;
            btnXem_KhachHang.Visibility = Visibility.Hidden;
            
        }
        public void TrangThaiThemKhachHang()
        {
            ResetTrangThaiKhachHang();
            btnThem_KhachHang.Visibility = Visibility.Visible;
            btnNext_KhachHang.Visibility = Visibility.Visible;
        }
        public void TrangThaiSuaKhachHang()
        {
            ResetTrangThaiKhachHang();
            cbbMaKhachHang_KhachHang.Visibility = Visibility.Visible;
            btnSua_KhachHang.Visibility = Visibility.Visible;
            btnXem_KhachHang.Visibility = Visibility.Visible;
        }
    }
}
